package com.zhuangjie.shop.handler.activity.handler.impl;

import com.zhuangjie.shop.entity.ShopActivityInstance;
import com.zhuangjie.shop.enums.ActivityType;
import com.zhuangjie.shop.handler.activity.ActivityEnv;
import com.zhuangjie.shop.handler.activity.handler.ActivityHandler;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 满减活动处理程序
 *
 * @author zhuangjie
 * @date 2024/06/14
 */
public class FullActivityHandler extends ActivityHandler {

    /**
     * 满减等级分隔符
     */
    private static final String FULL_REDUCTION_SEPARATOR = ",";

    @Override
    public ActivityType activityType() {
        return ActivityType.FULL_REDUCTION;
    }

    @Override
    public void checkActivityValid(ShopActivityInstance shopActivityInstance) {
        if (equalsActivityType(shopActivityInstance.getActivityId())) {
            throw new RuntimeException("活动类型不一致");
        }
        // 检查门槛类型是否是满减类型需要的结构
        String thresholds = shopActivityInstance.getThreshold();
        if (thresholds == null) {
            throw new RuntimeException("满减活动的门槛类型不能为空！");
        }
        try {
            Arrays.stream(thresholds.split(FULL_REDUCTION_SEPARATOR)).map(Double::valueOf);
        }catch (Exception e) {
            throw new RuntimeException("满减活动的门槛类型格式不正确！");
        }
        // 检查优惠值类型是否是满减类型需要的结构
        String values = shopActivityInstance.getValue();
        if (values == null) {
            throw new RuntimeException("满减活动的门槛类型不能为空！");
        }
        try {
            Arrays.stream(values.split(FULL_REDUCTION_SEPARATOR)).map(Double::valueOf);
        }catch (Exception e) {
            throw new RuntimeException("满减活动的优惠类型格式不正确！");
        }
        // 检查门槛与优惠值的size要是相同的
        if (thresholds.split(FULL_REDUCTION_SEPARATOR).length != values.split(FULL_REDUCTION_SEPARATOR).length) {
            throw new RuntimeException("满减活动的门槛类型与优惠类型数量不一致！");
        }


    }


    /**
     * 优惠计算之满减
     * @param env
     * @param shopActivityInstance
     * @return
     */
    @Override
    public BigDecimal calculateDiscountsHandler(ActivityEnv env, ShopActivityInstance shopActivityInstance) {
        // 1. 计算菜品总额（不要求特别精确，只用于比较，所以用double）
        double sum = env.getDishListSumPrice().doubleValue();
        // 2. 计算满足的满减级别（maxSatisfiedThresholdIndex）
        List<Double> thresholds = Arrays.stream(shopActivityInstance.getThreshold().split(FULL_REDUCTION_SEPARATOR)).map(Double::parseDouble).collect(Collectors.toList());

        int maxSatisfiedThresholdIndex = -1;
        Double maxSatisfiedThreshold = 0d;

        for (int i = 0; i < thresholds.size(); i++) {
            Double currentThreshold = thresholds.get(i);
            if(currentThreshold.compareTo(sum) <= 0 && currentThreshold > maxSatisfiedThreshold) {
                maxSatisfiedThreshold = currentThreshold;
                maxSatisfiedThresholdIndex = i;
            }
        }
        // 3. 获取对应的values优惠值返回
        if (maxSatisfiedThresholdIndex == -1) {
            return null;
        }
        List<String> values = Arrays.stream(shopActivityInstance.getValue().split(FULL_REDUCTION_SEPARATOR)).collect(Collectors.toList());
        return new BigDecimal(values.get(maxSatisfiedThresholdIndex));
    }


}
